## Replication code for Estimating Candidates’ Political Orientation in a Polarized Congress
## We use R for 3.2.4 GUI 1.67 Mavericks build (7152), running on Mac OS 10.12.1 

library(foreign) ## We use version: foreign_0.8-66
library(ggplot2) ## We use version: ggplot2_2.1.0
library(plyr)    ## We use version: plyr_1.8.4
library(dplyr)   ## We use version: dplyr_0.5.0
library(doBy)    ## We use version: doBy_4.5-14
library(xtable)  ## We use version: xtable_1.8-2


load(file=paste("candidatepositions_house_analysis.RData",sep=""))


# Multiple plot function
multiplot <- function(..., plotlist = NULL, file, cols = 1, layout = NULL) {
  require(grid)

                                        # Make a list from the ... arguments and plotlist
  plots <- c(list(...), plotlist)

  numPlots = length(plots)

                                        # If layout is NULL, then use 'cols' to determine layout
  if (is.null(layout)) {
                                        # Make the panel
                                        # ncol: Number of columns of plots
                                        # nrow: Number of rows needed, calculated from # of cols
    layout <- matrix(seq(1, cols * ceiling(numPlots/cols)), 
                     ncol = cols, nrow = ceiling(numPlots/cols))
  }

  if (numPlots == 1) {
    print(plots[[1]])

  } else {
                                        # Set up the page
    grid.newpage()
    pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))

                                        # Make each plot, in the correct location
    for (i in 1:numPlots) {
                                        # Get the i, j matrix positions of the regions that contain this subplot
      matchidx <- as.data.frame(which(layout ==  i, arr.ind = TRUE))

      print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row, 
                            layout.pos.col = matchidx$col))
    }
  }
}



################
## Application - polarization
################
analysis_all$election<-as.numeric(analysis_all$election)

#summary(lm(dwnom1_contemporaneous~ as.factor(election)*Party, data=analysis_all))
#summary(lm(dwnom1_contemporaneous~ as.factor(election)*cfscore, data=analysis_all))
#summary(lm(dwnom1_contemporaneous~ as.factor(election)*npat_score, data=analysis_all))

analysis_all$Party[analysis_all$Party==328]<-100
analysis_all$Party2<-as.vector(analysis_all$Party)
analysis_all$Party2[analysis_all$Party2=="100"] <- "Democrat"
analysis_all$Party2[analysis_all$Party2=="200"] <- "Republican"

## Data to show differences between groups

grouped <- dplyr::group_by(analysis_all,election,Party2)
grouped<-subset(grouped, Party2=="Democrat" | Party2=="Republican")
bonica_grouped<-dplyr::summarise(grouped, 
dwnom1_contemporaneous2=mean(dwnom1_contemporaneous, na.rm=T),
party2=mean(Party, na.rm = T),
shor_mccarty_score=mean(shor_mccarty_score, na.rm = T),
npat_score2=mean(npat_score, na.rm = T),
twitter_idealpoint2=mean(twitter_idealpoint, na.rm = T),
am_estimate2=mean(am_estimate, na.rm = T),
cfscore2=mean(cfscores.dyn, na.rm = T),
js_ideology_contemporaneous2=mean(js_ideology_contemporaneous, na.rm = T)
)

bonica_grouped_dems<-subset(bonica_grouped, Party2=="Democrat")
bonica_grouped_reps<-subset(bonica_grouped, Party2=="Republican")
bonica_grouped_reps<-subset(bonica_grouped_reps, election<2014)
bonica_grouped_dems<-subset(bonica_grouped_dems, election<2014)

bonica_grouped_diff_npat<-bonica_grouped_dems[,1]
bonica_grouped_diff_npat$diff<-bonica_grouped_dems$npat_score2- bonica_grouped_reps$npat_score2

bonica_grouped_diff_dwnom<-bonica_grouped_dems[,1]
bonica_grouped_diff_dwnom$diff<-bonica_grouped_dems$dwnom1_contemporaneous2- bonica_grouped_reps$dwnom1_contemporaneous2

bonica_grouped_diff_cfscore<-bonica_grouped_dems[,1]
bonica_grouped_diff_cfscore$diff<-bonica_grouped_dems$cfscore2- bonica_grouped_reps$cfscore2


###

 p <- ggplot(data = analysis_all[analysis_all$Party==100 | analysis_all$Party==200,], aes(x = election, y = dwnom1_contemporaneous,  colour=factor(Party2)))
p <-p + theme_bw()
#p <-p + geom_point()
#p <-p + geom_text()
#p <-p + geom_line()
p <-p + stat_smooth(method="loess", se=F)
p <-p + stat_summary(aes(colour=factor(Party2)),
    geom = "point", fun.y = mean,  size = 3)
p <-p + scale_x_continuous(limits=c(1980, 2012)#,breaks=c(1996, 1998 ,2000, 2002 , 2004, 2006, 2008, 2010, 2012), labels=c("1996", "1998" ,"2000", "2002" , "2004", "2006", "2008", "2010", "2012")
)
p <-p + scale_shape_manual(values=c(16,17))
p <-p +  scale_color_manual(values=c("black", "grey")) # c("blue","red")
p <-p +  theme(legend.position="none")
p <-p +  labs(x = "Year")
p <-p +  labs(y = "DW-Nominate Score")
p <-p +  labs(title = "Polarization in DW-Nominate
Scores (Incumbents)")
p <-p +    theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))
                     
p2 <- ggplot(data = bonica_grouped_diff_dwnom, aes(x = election, y = -diff))
p2 <-p2 + theme_bw()
#p2 <-p2 + geom_point()
#p2 <-p2 + geom_text()
#p2 <-p2 + geom_line()
p2 <-p2 + stat_smooth(method="loess", se=F,colour="gray45")
p2 <-p2 + scale_x_continuous(limits=c(1980, 2012)#,breaks=c(1996, 1998 ,2000, 2002 , 2004, 2006, 2008, 2010, 2012), labels=c("1996", "1998" ,"2000", "2002" , "2004", "2006", "2008", "2010", "2012")
)
p2 <-p2 +     scale_y_continuous(limits=c(0,1.25))
p2 <-p2 + scale_shape_manual(values=c(16,17))
p2 <-p2 +  scale_color_manual(values=c("black", "grey")) #c("blue", "red")
p2 <-p2 +  theme(legend.position="none")
p2 <-p2 +  labs(x = "Year")
p2 <-p2 +  labs(y = "DW-Nominate Score")
p2 <-p2 +  labs(title = "Polarization in DW-Nominate
Scores (Gap Between Parties)")
p2 <-p2 +    theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))
                     
 q <- ggplot(data = subset(analysis_all, !is.na(dwnom1_contemporaneous)), aes(x = election, y = cfscores.dyn, colour=factor(Party2)))
#q <-q + geom_point()
#q <-q + geom_line()
q <-q + theme_bw()
q <-q + stat_smooth(method="loess", se=F)
q <-q + stat_summary(aes(colour=factor(Party2)),
    geom = "point", fun.y = mean,  size = 3)
q <-q + scale_x_continuous(limits=c(1980, 2012)#,breaks=c(2000, 2002 , 2004, 2006, 2008, 2010, 2012), labels=c("2000", "2002" , "2004", "2006", "2008", "2010", "2012")
)
q <-q + scale_shape_manual(values=c(16,17))
q <-q +  scale_color_manual(values=c("black", "grey")) #c("blue", "red")
q <-q +  theme(legend.position="none")
q <-q +  labs(x = "Year")
q <-q +  labs(y = "CF-Score")
q <-q +  labs(title = "Polarization in Dynamic 
CF-Scores (Incumbents)")
q <-q +   theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))

 q2 <- ggplot(data = analysis_all, aes(x = election, y = cfscores.dyn, colour=factor(Party2)))
q2 <-q2 + theme_bw()
q2 <-q2 + stat_smooth(method="loess", se=F)
q2 <-q2 + stat_summary(aes(colour=factor(Party2)),
    geom = "point", fun.y = mean, shape = 16, size = 3)
q2 <-q2 + scale_x_continuous(limits=c(1980, 2012)#,breaks=c(2000, 2002 , 2004, 2006, 2008, 2010, 2012), labels=c("2000", "2002" , "2004", "2006", "2008", "2010", "2012")
)
q2 <-q2 + scale_shape_manual(values=c(16,16))
q2 <-q2 +  scale_color_manual(values=c("black", "grey")) #c("blue", "red")
q2 <-q2 +  theme(legend.position="none")
q2 <-q2 +  labs(x = "Year")
q2 <-q2 +  labs(y = "CF-Score")
q2 <-q2 +  labs(title = "Polarization in Dynamic 
CF-Scores (All Candidates)")
q2 <-q2 +  theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))

 q3 <- ggplot(data = bonica_grouped_diff_cfscore, aes(x = election, y = -diff))
q3 <-q3 + theme_bw()
q3 <-q3 + stat_smooth(method="loess", se=F,colour="gray45")
q3 <-q3 + scale_x_continuous(limits=c(1980, 2012)#,breaks=c(2000, 2002 , 2004, 2006, 2008, 2010, 2012), labels=c("2000", "2002" , "2004", "2006", "2008", "2010", "2012")
)
q3 <-q3 +     scale_y_continuous(limits=c(0,2.5))
q3 <-q3 + scale_shape_manual(values=c(16,16))
q3 <-q3 +  scale_color_manual(values=c("black", "grey")) #c("blue", "red")
q3 <-q3 +  theme(legend.position="none")
q3 <-q3 +  labs(x = "Year")
q3 <-q3 +  labs(y = "CF-Score")
q3 <-q3 +  labs(title = "Polarization in Dynamic 
CF-Scores (Gap Between Parties)")
q3 <-q3 +  theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))


 r <- ggplot(data = subset(analysis_all , !is.na(dwnom1_contemporaneous)), aes(x = election, y = npat_score, colour=factor(Party2)))
#r <-r + geom_point()
#r <-r + geom_line()
 r <-r + theme_bw()
r <-r + stat_smooth(method="loess", se=F)
r <-r + stat_summary(aes(colour=factor(Party2)),
    geom = "point", fun.y = mean, size = 3)
r <-r +  scale_x_continuous(limits=c(1980, 2012))
r <-r  +scale_shape_manual(values=c(16,17))
r <-r   + scale_color_manual(values=c("black", "grey")) #c("blue", "red")
r <-r   + theme(legend.position="none")
r <-r  + labs(x = "Year")
r <-r   + labs(y = "NPAT-Score")
r <-r  + labs(title = "Polarization in NPAT-Scores
(Incumbents)")
r <-r +    theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))

 r2 <- ggplot(data = analysis_all, aes(x = election, y = npat_score, colour=factor(Party2), shape=factor(Party2)))+ 
 theme_bw() + 
 stat_smooth(method="loess", se=F)+ 
 stat_summary(aes(colour=factor(Party2)),
    geom = "point", fun.y = mean,  size = 3)+ 
     scale_x_continuous(limits=c(1980, 2012))+
    scale_shape_manual(values=c(16,16)) + 
    scale_color_manual(values=c("black", "grey")) + #c("blue", "red")
    theme(legend.position="none") + 
    xlab("Year") + 
    ylab( "NPAT-Score") +
     labs(title = "Polarization in NPAT-Scores
 (All Candidates)") + 
    theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))

 r3 <- ggplot(data = bonica_grouped_diff_npat, aes(x = election, y = -diff))+ 
 theme_bw() + 
 stat_smooth(method="loess", se=F,colour="gray45")+ 

    scale_x_continuous(limits=c(1980, 2012))+
     scale_y_continuous(limits=c(0,2))+
   scale_shape_manual(values=c(16,16)) + 
    scale_color_manual(values=c("black", "grey")) + #c("blue", "red")
    theme(legend.position="none") + 
    xlab("Year") + 
    ylab( "NPAT-Score") +
     labs(title = "Polarization in NPAT-Scores
 (Gap Between Parties)") + 
    theme(plot.title = element_text(size = 9),axis.title.y =
                     element_text(size = 10), axis.text =
                 element_text(size = 8), axis.title.x =
                     element_text(size = 10))

### Figure 3             
pdf(file = paste("Figure3.pdf", sep = ""),
    width = 8, height = 5)
multiplot(p,q,r, p2,q3, r3, cols=2)
dev.off()

